package com.hxd.otc.module.my.activity;

import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bigkoo.pickerview.view.TimePickerView;
import com.hxd.otc.R;
import com.hxd.otc.base.BaseActivity;
import com.hxd.otc.common.UserHelper;
import com.hxd.otc.module.fiat_deal.bean.DealTeamBean;
import com.hxd.otc.module.my.adapter.NoSenseAdapter;
import com.hxd.otc.module.my.bean.NoSenseBean;
import com.hxd.otc.module.my.contract.NoSenseContract;
import com.hxd.otc.module.my.presenter.NoSensePresenter;
import com.hxd.otc.utils.DateUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class NoSenseBillActivity extends BaseActivity<NoSensePresenter> implements NoSenseContract.View {


    @BindView(R.id.tv_mouth)
    TextView tvMouth;

    @BindView(R.id.tv_coin)
    TextView tvCoin;

    @BindView(R.id.tv_sell_amount)
    TextView tvSellAmount;

    @BindView(R.id.tv_buy_amount)
    TextView tvBuyAmount;

    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    View errorView;

    View notDataView;

    TextView tvRefresh;

    NoSenseAdapter adapter;

    TimePickerView pvTime;

    OptionsPickerView pvOptions;

    String mouth;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_no_sense_bill;
    }

    @Override
    protected void initView() {
        adapter = new NoSenseAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                if (!TextUtils.isEmpty(tvCoin.getText().toString())) {
                    mPresenter.doLoadData(true, mouth, tvCoin.getText().toString(), UserHelper.getRoleLoginSource());
                } else {
                    mPresenter.setTime(mouth);
                    mPresenter.getCoinList();
                }


            }
        });

        notDataView = getLayoutInflater().inflate(R.layout.empty_view, (ViewGroup) recyclerView.getParent(), false);
        errorView = getLayoutInflater().inflate(R.layout.error_view, (ViewGroup) recyclerView.getParent(), false);
        tvRefresh = errorView.findViewById(R.id.tv_refresh);
        tvRefresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mRefreshLayout.autoRefresh(0, 10, 1);
            }
        });
        mouth = DateUtil.format(new Date(), DateUtil.YY_MM);
        tvMouth.setText(DateUtil.format(new Date(), DateUtil.MM));
    }

    @Override
    protected void initData() {
        mRefreshLayout.autoRefresh(0, 10, 1);
    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected NoSensePresenter createPresenter() {
        return new NoSensePresenter(this);
    }


    @Override
    public void onSetAdapter(NoSenseBean bean) {
        tvSellAmount.setText(bean.totalSellPrice);
        tvBuyAmount.setText(bean.totalBuyPrice);

        if (bean.nosenseOrderDetailVOList != null && bean.nosenseOrderDetailVOList.size() <= 0) {
            adapter.setEmptyView(notDataView);
        }
        adapter.setNewData(bean.nosenseOrderDetailVOList);
    }

    @Override
    public void onShowNoMore() {
        if (mRefreshLayout != null)
            mRefreshLayout.finishRefresh();
    }

    @Override
    public void setCoinList(ArrayList<DealTeamBean> dealTeamBeans) {
        tvCoin.setText(dealTeamBeans.get(0).symbol);
        for (int i = 0; i < dealTeamBeans.size(); i++) {
            if (dealTeamBeans.get(i).symbol.equals(getString(R.string.EUC))) {
                tvCoin.setText(dealTeamBeans.get(i).symbol);
                break;
            }
        }
        mPresenter.doLoadData(true, mouth, tvCoin.getText().toString(),UserHelper.getRoleLoginSource());
        initCoinPicker(dealTeamBeans);
    }


    @Override
    public void onError(int code) {
        super.onError(code);
        adapter.setEmptyView(errorView);
    }

    @OnClick({R.id.tv_mouth, R.id.tv_coin})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_mouth:
                initTimePicker();
                break;
            case R.id.tv_coin:
                if (pvOptions == null) {
                    showToast(getString(R.string.net_error));
                } else {
                    pvOptions.show();
                }

                break;
        }
    }

    private void initCoinPicker(ArrayList<DealTeamBean> dealTeamBeans) {
        if (pvOptions == null) {
            final List<String> options1Items = new ArrayList<>();
            for (int i = 0; i < dealTeamBeans.size(); i++) {
                options1Items.add(dealTeamBeans.get(i).symbol);
            }
            pvOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
                @Override
                public void onOptionsSelect(int options1, int option2, int options3, View v) {
                    tvCoin.setText(options1Items.get(options1));
                    mRefreshLayout.autoRefresh(0, 10, 1);
                }
            })
                    .setTitleText(getString(R.string.choose_coin))
                    .setTitleColor(getResources().getColor(R.color.color_EditText_text))
                    .setTitleSize(18)
                    .setContentTextSize(20)
                    .setSubmitColor(getResources().getColor(R.color.color_text1))
                    .build();
            pvOptions.setPicker(options1Items);
        }
    }


    private void initTimePicker() {
        if (pvTime == null) {
            pvTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
                @Override
                public void onTimeSelect(Date date, View v) {
                    mouth = DateUtil.format(date, DateUtil.YY_MM);
                    tvMouth.setText(DateUtil.format(date, DateUtil.MM));
                    mRefreshLayout.autoRefresh(0, 10, 1);
                }
            })
                    .setTitleText(getString(R.string.choose_mouth))
                    .setTitleColor(getResources().getColor(R.color.color_EditText_text))
                    .setTitleSize(18)
                    .setContentTextSize(20)
                    .setSubmitColor(getResources().getColor(R.color.color_text1))
                    .setLabel(getString(R.string.year), getString(R.string.mouth), "", "", "", "")
                    .setType(new boolean[]{true, true, false, false, false, false}).build();
        }
        pvTime.show();
    }
}
